package org.jeecgframework.youmeng;

import org.jeecgframework.youmeng.android.*;
import org.jeecgframework.youmeng.ios.*;
import org.json.JSONArray;
import org.json.JSONObject;

public class Demo {
	private String appkey = "5618876a67e58ed23b0058b2";
	private String appMasterSecret = "jlncfkoxkob4a896gvor0bri0ukyz2bu";
	private String timestamp = null;
	private PushClient client = new PushClient();
	
	public Demo(String key, String secret) {
		try {
			appkey = key;
			appMasterSecret = secret;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void sendAndroidBroadcast() throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
		broadcast.setTicker("服务器推送广播");
		broadcast.setTitle("服务器推送广播");
		broadcast.setText("服务器推送广播");
		broadcast.goAppAfterOpen();
		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
//		broadcast.setProductionMode();
		broadcast.setTestMode();
		// Set customized fields
		broadcast.setExtraField("xiaoji", "helloworld");
		client.send(broadcast);
	}

	/**
	 * 单播
	 * @throws Exception
	 */
	public void sendAndroidUnicast() throws Exception {
		AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken("Aj-xVty0NjfbihxASg3n5Ah-3gldPQxdEidyX26blOsB");
		unicast.setTicker("通知提示");
		unicast.setTitle("小吉测试单个推送");
		unicast.setText("小吉内容");
		unicast.goAppAfterOpen();
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
//		unicast.setProductionMode();
		unicast.setTestMode();
		// Set customized fields
		unicast.setExtraField("test", "helloworld");
		client.send(unicast);
	}

	/**
	 * groupcast-组播
	 (按照filter条件筛选特定用户群, 具体请参照filter参数)
	 * @throws Exception
	 */
	public void sendAndroidGroupcast() throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  Construct the filter condition:
		 *  "where": 
		 *	{
    	 *		"and": 
    	 *		[
      	 *			{"tag":"test"},
      	 *			{"tag":"Test"}
    	 *		]
		 *	}
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		JSONObject TestTag = new JSONObject();
		testTag.put("tag", "test");
		TestTag.put("tag", "Test");
		tagArray.put(testTag);
		tagArray.put(TestTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());
		
		groupcast.setFilter(filterJson);
		groupcast.setTicker( "Android groupcast ticker");
		groupcast.setTitle(  "中文的title");
		groupcast.setText(   "Android groupcast text");
		groupcast.goAppAfterOpen();
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		groupcast.setProductionMode();
		client.send(groupcast);
	}

	/**
	 *  customizedcast(通过开发者自有的alias进行推送),
	 包括以下两种case:
	 - alias: 对单个或者多个alias进行推送
	 - file_id: 将alias存放到文件后，根据file_id来推送
	 * @throws Exception
	 */
	public void sendAndroidCustomizedcast() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then 
		// use file_id to send customized notification.
		customizedcast.setAlias("1211A0FBC73EB762B416D4B50A9FEB681F,12BEFFACCAF99AFCEFE18712563E49C3C4", AliasType.XINXUN_CLIENT.getValue());
		customizedcast.setTicker( "小吉50个以内调用该方法");
		customizedcast.setTitle(  "小吉50个以内调用该方法");
		customizedcast.setText(   "小吉50个以内调用该方法");
		customizedcast.goAppAfterOpen();
//		customizedcast.setAlias("","");//打开app的时候设置参数
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
//		customizedcast.setProductionMode();
		customizedcast.setTestMode();
		client.send(customizedcast);
	}

	/**
	 * 可选 当type=filecast时，file内容为多条device_token,
	 device_token以回车符分隔
	 当type=customizedcast时，file内容为多条alias，
	 alias以回车符分隔，注意同一个文件内的alias所对应
	 的alias_type必须和接口参数alias_type一致。
	 注意，使用文件播前需要先调用文件上传接口获取file_id,
	 具体请参照"2.4文件上传接口"
	 * @throws Exception
	 */
	public void sendAndroidCustomizedcastFile() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then 
		// use file_id to send customized notification.
		String fileId = client.uploadContents(appkey,appMasterSecret,"1211A0FBC73EB762B416D4B50A9FEB681F"+"\n"+"12BEFFACCAF99AFCEFE18712563E49C3C4"+"\n");
		customizedcast.setFileId(fileId, AliasType.XINXUN_CLIENT.getValue());
		customizedcast.setTicker("file内容为多条alias");
		customizedcast.setTitle("小吉file内容为多条alias");
		customizedcast.setText("type=customizedcast时，file内容为多条alias");
		customizedcast.goAppAfterOpen();
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
//		customizedcast.setProductionMode();
		customizedcast.setTestMode();
		client.send(customizedcast);
	}
	
	public void sendAndroidFilecast() throws Exception {
		AndroidFilecast filecast = new AndroidFilecast(appkey,appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens 
		String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
		filecast.setFileId( fileId);
		filecast.setTicker( "Android filecast ticker");
		filecast.setTitle(  "中文的title");
		filecast.setText(   "Android filecast text");
		filecast.goAppAfterOpen();
		filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		client.send(filecast);
	}
	
	public void sendIOSBroadcast() throws Exception {
		IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);

		broadcast.setAlert("小吉IOS广播测试");
		broadcast.setBadge(0);
		broadcast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		broadcast.setTestMode();
		// Set customized fields
		broadcast.setCustomizedField("test", "helloworld");
		client.send(broadcast);
	}
	
	public void sendIOSUnicast() throws Exception {
		IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
		// TODO Set your device token
		//山哥手机
//		unicast.setDeviceToken("157e7eda77adff2fa585a9cd501b370d92a4c9d7eb905b2aa36d4e0d22f8c3b5");
		//小钟手机
		unicast.setDeviceToken("259a33c149891f5a38edea4cc6e619af71df48f133afc1831d73911576a8dc83");
		unicast.setAlert("小吉IOS文章单播测试");
		unicast.setBadge( 0);
		unicast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setTestMode();
		// Set customized fields
		unicast.setCustomizedField("id", "135587");
		unicast.setCustomizedField("type", "0");
		client.send(unicast);
	}
	
	public void sendIOSGroupcast() throws Exception {
		IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  Construct the filter condition:
		 *  "where": 
		 *	{
    	 *		"and": 
    	 *		[
      	 *			{"tag":"iostest"}
    	 *		]
		 *	}
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		testTag.put("tag", "iostest");
		tagArray.put(testTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());
		
		// Set filter condition into rootJson
		groupcast.setFilter(filterJson);
		groupcast.setAlert("IOS 组播测试");
		groupcast.setBadge( 0);
		groupcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		groupcast.setTestMode();
		client.send(groupcast);
	}
	
	public void sendIOSCustomizedcast() throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
		// TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then 
		// use file_id to send customized notification.
		customizedcast.setAlias("11oImVDuBIqGRRPFjH_tVy_W67AJDg,11oImVDuHbnQ8cmRMGRYsmcttVu77w", AliasType.XINXUN_CLIENT.getValue());
		customizedcast.setAlert("小吉IOS别名发送测试");
		customizedcast.setBadge( 0);
		customizedcast.setSound( "default");
//		customizedcast.setCustomizedField("","");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		customizedcast.setTestMode();
		client.send(customizedcast);
	}
	
	public void sendIOSFilecast() throws Exception {
		IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens 
		String fileId = client.uploadContents(appkey,appMasterSecret,"11oImVDuBIqGRRPFjH_tVy_W67AJDg"+"\n"+"11oImVDuHbnQ8cmRMGRYsmcttVu77w");
		filecast.setFileId(fileId);
		filecast.setAlert("小吉IOS 50个以上通知用这个 测试");
		filecast.setBadge(0);
		filecast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		filecast.setTestMode();
		client.send(filecast);
	}
	public void sendIOSCustomizedcastFile() throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
		// TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then
		// use file_id to send customized notification.
		String fileId = client.uploadContents(appkey,appMasterSecret,"11oImVDuBIqGRRPFjH_tVy_W67AJDg"+"\n"+"11oImVDuHbnQ8cmRMGRYsmcttVu77w");
		customizedcast.setFileId(fileId, AliasType.XINXUN_CLIENT.getValue());
		customizedcast.setAlert("小吉IOS 50个以上通知用这个 测试");
		customizedcast.setBadge( 0);
		customizedcast.setSound( "default");
//		customizedcast.setCustomizedField("","");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		customizedcast.setTestMode();
		client.send(customizedcast);
	}
	
	public static void main(String[] args) {
		// TODO set your appkey and master secret here
		//android
//		Demo demo = new Demo("5618876a67e58ed23b0058b2", "jlncfkoxkob4a896gvor0bri0ukyz2bu");
		//ios
		Demo demo = new Demo("561887eae0f55a560f00265d", "ud66otmzimhzqdtdscavezpwgzunru1h");
		try {
			//android
//			demo.sendAndroidUnicast();
//			demo.sendAndroidCustomizedcastFile();
//			demo.sendAndroidCustomizedcast();
//			demo.sendAndroidBroadcast();
			//ios
//			demo.sendIOSUnicast();
//			demo.sendIOSBroadcast();
//			demo.sendIOSCustomizedcast();
//			demo.sendIOSFilecast();
			demo.sendIOSCustomizedcastFile();
			/* TODO these methods are all available, just fill in some fields and do the test
			 * demo.sendAndroidCustomizedcastFile();
			 * demo.sendAndroidBroadcast();
			 * demo.sendAndroidGroupcast();
			 * demo.sendAndroidCustomizedcast();
			 * demo.sendAndroidFilecast();
			 * 
			 * demo.sendIOSBroadcast();
			 * demo.sendIOSUnicast();
			 * demo.sendIOSGroupcast();
			 * demo.sendIOSCustomizedcast();
			 * demo.sendIOSFilecast();
			 */
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	

}
